<?php

namespace app\controller\system;

use app\BaseController;
use app\model\SupplierPriceRule;
use app\model\SupplierStation;
use think\response\Json;

class Supplier extends BaseController
{
    public function save(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\Supplier::class)->saveData();
        });
    }

    public function lst(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\Supplier::class)->pageData();
        });
    }

    public function detail(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\Supplier::class)->detailData();
        });
    }

    public function balance()
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\Supplier::class)->balance();
        });
    }

    public function balance_lst(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\SupplierBalanceNotice::class)->pageData();
        });
    }

    public function search(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\Supplier::class)->search();
        });
    }

    public function supplier_station_ids(): Json
    {
        return $this->handle(function () {
            return $this->app->make(\app\model\SupplierStation::class)->supplier_station_ids();
        });
    }

    public function station()
    {
        return $this->handle(function () {
            return $this->app->make(SupplierStation::class)->saveStation();
        });
    }

    public function station_lst(): Json
    {
        return $this->handle(function () {
            return $this->app->make(SupplierStation::class)->pageData();
        });
    }

    public function priceRuleSave(): Json
    {
        return $this->handle(function () {
            return $this->app->make(SupplierPriceRule::class)->saveData();
        });
    }

    public function priceRuleLst(): Json
    {
        return $this->handle(function () {
            return $this->app->make(SupplierPriceRule::class)->pageData();
        });
    }

    public function priceRuleDetail(): Json
    {
        return $this->handle(function (){
            return $this->app->make(SupplierPriceRule::class)->detailData();
        });
    }

    public function deleteRule(): Json
    {
        return $this->handle(function () {
            $id = request()->param('id');
            return $this->app->make(SupplierPriceRule::class)->where('id', $id)->delete();
        });
    }

    public function updateRuleState(): Json
    {
        return $this->handle(function () {
            $id = request()->param('id');
            $state = request()->param('state');
            return $this->app->make(SupplierPriceRule::class)->where('id', $id)->update(['state' => $state]);
        });
    }

    public function rule_config(): Json
    {
        return $this->handle(function () {
            $conf = $this->request->post();
            $conf['start_time'] = $conf['start_time']." 00:00:00";
            $its = "{".PHP_EOL."\t\"name\": \"$conf[name]\",".PHP_EOL."\t\"start_time\": \"$conf[start_time]\",".PHP_EOL."\t\"end_time\": \"$conf[end_time]\",".PHP_EOL."\t\"price\": $conf[price],".PHP_EOL."\t\"state\": $conf[state]".PHP_EOL."}";
            if(strtotime($conf['start_time']) > time()){
                //规则暂时未到生效时间，存入setting.price.conf中
                file_put_contents("./station_config/setting.price.conf",$its);
            } else {
                file_put_contents("./station_config/price.conf",$its);
            }
            return $conf;
        });
    }
}
